package com.wellview.template.modle;


import android.telecom.Call;
import android.text.TextUtils;


import com.wellview.template.App;
import com.wellview.template.Constant;
import com.wellview.template.bean.StartBean;
import com.wellview.template.logger.Logger;
import com.wellview.template.presenter.BaseProvider;
import com.wellview.template.presenter.SplashProvider;
import com.wellview.template.utils.Utils;
import com.wellview.template.utils.XulUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * Created by jizi.chen on 2017/8/28.
 */

public class SplashBehavior extends BaseBehavior {

    private OnStartResponse startListener;

    public SplashBehavior() {
        super(SplashProvider.class);
    }

    public void setStartListener(OnStartResponse listener) {
        this.startListener = listener;
    }

    public interface OnStartResponse {
        void startResponseSuccess(List<StartBean> beans);
        void startResponseFailed(Exception e);
    }

    public void requestStartPage() {
        Clause startClause = new Clause().method(Clause.Method.GET);
        getProvider()
                .setUrl(Constant.URL_Start_API)
                .addClause(startClause)
                .execute(new BaseProvider.ResultListener<String>() {
                    @Override
                    public void inProgress(float progress, long total, int id) {

                    }

                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        if (startListener != null){
                            startListener.startResponseFailed(e);
                        }
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Logger.i(TAG, "Thread thread " + Thread.currentThread().getName());
                        parseStartImage(response);
                    }
                });


    }

    private void parseStartImage(String response) {
        if (TextUtils.isEmpty(response)){
            if (startListener != null){
                startListener.startResponseFailed(new Exception("response is null"));
            }
            return;
        }
        JSONObject startJson = null;
        try {
            startJson = new JSONObject(response);
        } catch (JSONException e) {
            Logger.i(TAG, " parse json error ");
            if (startListener != null){
                startListener.startResponseFailed(new Exception("response is not a json"));
            }
            e.printStackTrace();
            return;
        }
        List<StartBean> startBeanList = new ArrayList<>();
        JSONObject dataObject = startJson.optJSONObject("Data");
        if (dataObject != null){
            JSONArray adverts = dataObject.optJSONArray("adverts");
            if (adverts != null && adverts.length()> 0){
                for (int i = 0; i < adverts.length(); i++) {
                    StartBean startBean = new StartBean();
                    JSONObject advert = adverts.optJSONObject(i);
                    startBean.setNetPath(advert.optString("netPath"));
                    startBean.setSort(advert.optInt("sort"));
                    startBean.setLinkAddress(advert.optString("linkAddress"));
                    startBeanList.add(startBean);
                }
            }
        }
        Collections.sort(startBeanList,new Comparator<StartBean>() {
            @Override
            public int compare(StartBean o1, StartBean o2) {
                return o1.getSort()-o2.getSort();
            }
        });
        if (startListener != null){
            startListener.startResponseSuccess(startBeanList);
        }
        Utils.writeObject(App.getInstance(),"splash", "home.txt", startBeanList);

    }
}
